use super::utils::{read_response_as_json, send_request};
use serde_json::json;

#[actix_rt::test]
pub async fn test_markdown() {
    let resp = send_request(
        "POST",
        "/markdown",
        json!({"title": "test kn", "content": "test content" }).into(),
    )
    .await;

    assert_eq!(resp.status(), 200);
    let response_json = read_response_as_json(resp).await;
    assert!(response_json["result"]["id"].as_str().unwrap_or("").len() > 0);
}

#[actix_rt::test]
pub async fn test_get_markdown_titles() {
    let resp = send_request("GET", "/markdown_titles", None).await;

    assert_eq!(resp.status(), 200);
    let response_json = read_response_as_json(resp).await;
    assert!(response_json["result"]["list"].as_array().unwrap().len() > 1);
}

#[actix_rt::test]
pub async fn test_delete_markdown() {
    let resp = send_request(
        "POST",
        "/markdown",
        json!({"title": "test kn", "content": "test content"}).into(),
    )
    .await;
    let response_json = read_response_as_json(resp).await;
    let id: &str = response_json["result"]["id"]
        .as_str()
        .expect("test_delete_markdown to id error");
    let resp = send_request("DELETE", &format!("/markdown/{}", id), None).await;

    assert_eq!(resp.status(), 200);
    let response_json1 = read_response_as_json(resp).await;
    let delete_count = response_json1["result"]["deleted_count"]
        .as_i64()
        .expect("delete_count field missing");
    assert_eq!(delete_count, 1);
}

#[actix_rt::test]
pub async fn test_get_markdown() {
    let resp = send_request(
        "POST",
        "/markdown",
        json!({"title": "test kn", "content": "test content"}).into(),
    )
    .await;

    assert_eq!(resp.status(), 200);
    let response_json = read_response_as_json(resp).await;
    let id = response_json["result"]["id"].as_str().unwrap_or("");

    let resp1 = send_request("GET", &format!("/markdown/{}", id), None).await;

    assert_eq!(resp1.status(), 200);
    let response_json1 = read_response_as_json(resp1).await;
    assert!(
        response_json1["result"]["content"]
            .as_str()
            .unwrap_or("")
            .len()
            > 0
    );
}
